/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.sheet.validate;

import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

import twaver.table.editor.ActionTableCellEditor;

public class ScaleEditor extends ActionTableCellEditor{

	private JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
	
	public ScaleEditor(){
		super(new ScaleRenderer());
	}

	public void actionPerformed(ActionEvent e) {
		Object value = this.getValue();
		
        if(value instanceof Number){ 
        	int intValue = (int)(((Number)value).doubleValue() * 100);
        	slider.setValue(intValue);
        } 
         
        Window window = (Window)SwingUtilities.getAncestorOfClass(Window.class, getActionButton());
        int result = JOptionPane.showOptionDialog(window 
                , slider, (String)getTable().getColumnName(getColumn()) + "[" + value + "]" 
                , JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); 
        
        if(result==JOptionPane.OK_OPTION) {
        	this.setValue(new Float(slider.getValue()/100.0));
            this.stopCellEditing();
        }else{
        	this.cancelCellEditing();
        }
	}


}